<template>
  <div>
    <!--:close-on-click-modal="false"-->
    <el-dialog :title="actionType=='add'?'新增'+title:'修改'+title" :visible.sync="dialogFormVisible" top="10vh"
    >
      <el-form :model="dialogFormData" ref="dialogFormData">
        <lemon-e-form :form-attr="dialogFormParam.formAttr" :cols="dialogFormParam.cols"></lemon-e-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd" v-if="actionType=='add'" :loading="loading">保存</el-button>
        <el-button type="primary" @click="handleEdit" v-else :loading="loading">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss">

</style>

<script>
  import LemonEForm from '../../LemonEForm'

  export default {
    name: 'LemonEDialogForm',
    components: {LemonEForm},
    props: {
      /*
      dialogFormParam:{formAttr:{},cols:''}
      */
      title: String,
      addMethod: Function,
      getMethod: Function,
      updateMethod: Function,
      dialogFormParam: Object
    },
    data() {
      return {
        actionType: 'add',
        dialogFormVisible: false,
        loading: false,
        dialogFormData: {},
        id: ''
      }
    },
    methods: {
      initTableSearchParam() {
        for (const sc of this.dialogFormParam.formAttr.items) {
          this.$set(this.dialogFormData, sc.code, sc.value)
        }
      },
      addData() {
        this.clearDialogForm()
        this.actionType = 'add'
        this.dialogFormVisible = true
      },
      handleAdd() {
        this.initTableSearchParam()
        this.loading = true
        this.addMethod(this.dialogFormData, (res) => {
          this.loading = false
          this.dialogFormVisible = false
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.$emit('refreshTable')
        }, () => {
          this.loading = false
        })
      },
      editData(row) {
        this.clearDialogForm()
        this.actionType = 'edit'
        this.id = row.id
        this.getMethod(row.id, (res) => {
          for (const sc of this.dialogFormParam.formAttr.items) {
            for (let v in res) {
              if (v === sc.code) {
                sc.value = res[v]
                break
              }
            }
          }
          this.dialogFormVisible = true
        }, () => {
        })
      },
      handleEdit() {
        this.initTableSearchParam()
        this.loading = true
        this.updateMethod(this.id, this.dialogFormData, (res) => {
          this.loading = false
          this.dialogFormVisible = false
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.$emit('refreshTable')
        }, () => {
          this.loading = false
        })
      },
      clearDialogForm() {
        for (const sc of this.dialogFormParam.formAttr.items) {
          console.log(sc)
          //隐藏列有默认值，不用进行清空
          if (typeof(sc.hidden) === 'undefined' || !sc.hidden) {
            sc.value = ''
          }

        }
      }
    }
  }
</script>
